﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.Utils;
using DevExpress.XtraCharts;
using DevExpress.XtraEditors;
using QunYing.BLL;

namespace Client
{
    public partial class FrmBusiness : BaseChildForm
    {
        public FrmBusiness()
        {
            InitializeComponent();
            dateSpan.StartDateEdit.Properties.DisplayFormat.FormatString = "yyyy-MM";
            dateSpan.StartDateEdit.Properties.DisplayFormat.FormatType = FormatType.DateTime;
            dateSpan.EndDateEdit.Properties.DisplayFormat.FormatString = "yyyy-MM";
            dateSpan.EndDateEdit.Properties.DisplayFormat.FormatType = FormatType.DateTime;

            dateSpan.StartDateEdit.DateTime = DateTime.Now.AddMonths(-6).Date;
            dateSpan.EndDateEdit.DateTime = DateTime.Now.Date;
            cbxType.SelectedIndex = 0;
        }
        public override void BindData()
        {
            DrawChart();
        }
        private void FrmBusiness_Load(object sender, EventArgs e)
        {
            HideCbx();
            CbxBind();
            DrawChart();
        }

        private void btnQuery_Click(object sender, EventArgs e)
        {
            DrawChart();
        }
        private void CbxBind()
        {
            DataTable dtAll = new DataTable();
            dtAll.Columns.Add("经销商");
            if (GlobalInfo.UserLoginInfo.角色 == "经销商")
            {
                dtAll.Rows.Add(GlobalInfo.UserLoginInfo.用户ID);
            }
            else
            {
                QunYing.BLL.UserLoginInfo user = new UserLoginInfo();
                DataTable dt = user.GetList("角色='经销商'").Tables[0];

                dtAll.Rows.Add("");
                foreach (DataRow dr in dt.Rows)
                {
                    dtAll.Rows.Add(dr["用户ID"]);
                }
            }

            cbxDealer.Properties.DataSource = dtAll;
            cbxDealer.Properties.ValueMember = "经销商";
            cbxDealer.Properties.DisplayMember = "经销商";
            cbxDealer.ItemIndex = 0;
        }

        private void HideCbx()
        {
            if (GlobalInfo.UserLoginInfo.角色 == "经销商")
            {
                cbxDealer.Visible = false;
                lblDealer.Visible = false;
                btnQuery.Left = btnQuery.Left - 195;
            }
        }
        private void DrawChart()
        {
            if (this.dateSpan.StartDateEdit.EditValue == null || this.dateSpan.EndDateEdit.EditValue == null)
            {
                GlobalInfo.MsgBoxShowInfo("请选择统计日期段!");
                return;
            }
            //string isDealer = GlobalInfo.UserLoginInfo.角色 == "经销商" ? "true" : "false";
            string isDealer = cbxDealer.EditValue.ToString() == "" ? "false" : "true";
            DateTime startDate = dateSpan.StartDateEdit.DateTime;
            DateTime endDate = dateSpan.EndDateEdit.DateTime;
            QunYing.BLL.VipInfo info = new QunYing.BLL.VipInfo();
           //DataSet ds = info.QueryVip(GlobalInfo.UserLoginInfo.用户ID, startDate, endDate, isDealer);
            DataSet ds = info.QueryVip(cbxDealer.EditValue.ToString(), startDate, endDate, isDealer);
            if (cbxType.EditValue.ToString() == "新增会员数")
            {
                chartControl1.BeginInit();
                chartControl1.Series.Clear();
                Series series1 = new Series("series1", ViewType.Bar);
                series1.LegendText = "新增会员数";
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    series1.Points.Add(new SeriesPoint(ds.Tables[0].Rows[i]["月份"], ds.Tables[0].Rows[i]["新增会员数"]));
                }
                chartControl1.Series.Add(series1);
            }
            else if (cbxType.EditValue.ToString() == "收费金额")
            {

                chartControl1.BeginInit();
                chartControl1.Series.Clear();
                Series series1 = new Series("series1", ViewType.Bar);
                series1.LegendText = "收费金额";
                for (int i = 0; i < ds.Tables[1].Rows.Count; i++)
                {
                    series1.Points.Add(new SeriesPoint(ds.Tables[1].Rows[i]["月份"], ds.Tables[1].Rows[i]["收费金额"]));
                }
                chartControl1.Series.Add(series1);
            }
            chartControl1.EndInit();
        }
    }
}